package com.hyy.customer.controller;

import java.util.List;

import com.hyy.api.domain.customer.bo.CstmCustomerBo;
import com.hyy.api.domain.customer.bo.CstmCustomerProviderChannelBo;
import com.hyy.api.domain.customer.vo.CstmCorporateVo;
import com.hyy.api.domain.customer.vo.CstmCustomerProviderChannelVo;
import com.hyy.api.domain.customer.vo.CstmCustomerVo;
import com.hyy.api.domain.provider.bo.BindProviderChannelBo;
import com.hyy.common.mybatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.hyy.common.idempotent.annotation.RepeatSubmit;
import com.hyy.common.log.annotation.Log;
import com.hyy.common.web.core.BaseController;
import com.hyy.common.mybatis.core.page.PageQuery;
import com.hyy.common.core.domain.R;
import com.hyy.common.core.validate.AddGroup;
import com.hyy.common.core.validate.EditGroup;
import com.hyy.common.log.enums.BusinessType;
import com.hyy.common.excel.utils.ExcelUtil;
import com.hyy.customer.service.ICstmCustomerService;

/**
 * 下游客户
 *
 * @author ruoyi
 * @date 2025-05-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/customer/customer")
public class CstmCustomerController extends BaseController {

    private final ICstmCustomerService cstmCustomerService;

    /**
     * 查询下游客户列表
     */
    @SaCheckPermission("customer:customer:list")
    @GetMapping("/list")
    public TableDataInfo<CstmCustomerVo> list(CstmCustomerBo bo, PageQuery pageQuery) {
        return cstmCustomerService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出下游客户列表
     */
    @SaCheckPermission("customer:customer:export")
    @Log(title = "下游客户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CstmCustomerBo bo, HttpServletResponse response) {
        List<CstmCustomerVo> list = cstmCustomerService.queryList(bo);
        ExcelUtil.exportExcel(list, "下游客户", CstmCustomerVo.class, response);
    }

    /**
     * 获取下游客户详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("customer:customer:query")
    @GetMapping("/{id}")
    public R<CstmCustomerVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(cstmCustomerService.queryById(id));
    }

    /**
     * 新增下游客户
     */
    @SaCheckPermission("customer:customer:add")
    @Log(title = "下游客户", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CstmCustomerBo bo) {
        return toAjax(cstmCustomerService.insertByBo(bo));
    }



    /**
     * 获取新客户账号
     */
    @SaCheckPermission("customer:customer:add")
    @RepeatSubmit()
    @GetMapping("/new")
    public R<CstmCustomerVo> newCustomer() {
        return  R.ok(cstmCustomerService.newCustomer());
    }



    /**
     * 修改下游客户
     */
    @SaCheckPermission("customer:customer:edit")
    @Log(title = "下游客户", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CstmCustomerBo bo) {
        return toAjax(cstmCustomerService.updateByBo(bo));
    }

    /**
     * 删除下游客户
     *
     * @param ids 主键串
     */
    @SaCheckPermission("customer:customer:remove")
    @Log(title = "下游客户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(cstmCustomerService.deleteWithValidByIds(List.of(ids), true));
    }


    /**
     * 获取供应商通道
     */
    @SaCheckPermission("customer:customer:bindProviderChannel")
    @GetMapping("getProviderChannel/{customerId}")
    public R<List<CstmCustomerProviderChannelVo>> getProviderChannel(@PathVariable("customerId") Long customerId) {
        return R.ok(cstmCustomerService.getProviderChannel(customerId));
    }


    /**
     * 绑定供应商通道
     */
    @SaCheckPermission("customer:customer:bindProviderChannel")
    @Log(title = "绑定供应商通道", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("bindProviderChannel")
    public R<Void> bindProviderChannel(@RequestBody BindProviderChannelBo bo) {
        return toAjax(cstmCustomerService.bindProviderChannel(bo));
    }


}
